package com.drinks.yiniang.ui.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * line 上画圆点
 * @author han
 * @date 2019/11/14
 */
public class DotLineView extends View {
    private int mLineColor;
    private int mLineHeight;

    private int mDotColor;
    private int mDotRadius;
    //线条方向, 水平或横向
    private int mOrientation;

    //dot前面是否有线条
    private boolean hasStartLine;
    //dot后面是否有线条
    private boolean hasEndLine;

    private Paint mLinePaint;
    private Paint mDotPaint;
    private Path mPath;

    public DotLineView(Context context) {
        this(context, null);
    }

    public DotLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DotLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mOrientation = LinearLayout.VERTICAL;

        mLineColor = ColorUtils.getColorById(getContext(), com.hansen.library.R.color.color_line);
        mLineHeight = 2;

        mDotColor = mLineColor;
        mDotRadius = 8;

        hasStartLine = true;
        hasEndLine = true;

        mLinePaint = new Paint();
        mDotPaint = new Paint();

        mPath = new Path();

        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(mLineColor);
        mLinePaint.setStrokeWidth(mLineHeight);
        mLinePaint.setAntiAlias(true);

        mDotPaint.setStyle(Paint.Style.FILL);
        mDotPaint.setColor(mLineColor);
        mDotPaint.setStrokeWidth(mLineHeight);
        mDotPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(mOrientation == LinearLayout.VERTICAL) {
            int heightSize = measureHeight(heightMeasureSpec);

            setMeasuredDimension(mDotRadius * 2 > mLineHeight ? mDotRadius * 2 : mLineHeight, heightSize);
        } else {
            int widthSize = measureWidth(widthMeasureSpec);

            setMeasuredDimension(widthSize, mDotRadius * 2 > mLineHeight ? mDotRadius * 2 : mLineHeight);
        }
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //设置一个默认值，就是这个View的默认宽度为1，这个看我们自定义View的要求
        int result = 2;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }

        return result > mDotRadius * 2 ? result : mDotRadius * 2;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 2;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }

        return result > mDotRadius * 2 ? result : mDotRadius * 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPath.reset();

        int centX = getWidth() / 2;
        int centY = getHeight() / 2;

        int tempLineHeight = mDotRadius * 2 > mLineHeight ? mDotRadius * 2 : mLineHeight;

        if(mOrientation == LinearLayout.VERTICAL) {//竖线
            int oneWidth = (getHeight() - mDotRadius * 2) / 2;


            if(hasStartLine) {//画dot前面线
                mPath.moveTo((float) (0 + tempLineHeight / 2.0), 0);
                mPath.lineTo((float) (0 + tempLineHeight / 2.0), oneWidth);
            }

            if(hasEndLine) {//画dot后部分线
                mPath.moveTo((float) (0 + tempLineHeight / 2.0), getHeight() - oneWidth);
                mPath.lineTo((float) (0 + tempLineHeight / 2.0), getHeight());
            }
        } else {//横线
            int oneWidth = (getWidth() - mDotRadius * 2) / 2;

            if(hasStartLine) {
                mPath.moveTo(0, (float) (0 + tempLineHeight / 2.0));
                mPath.lineTo(oneWidth, (float) (0 + tempLineHeight / 2.0));
            }

            if(hasEndLine) {
                mPath.moveTo(oneWidth, (float) (0 + tempLineHeight / 2.0));
                mPath.lineTo(getWidth() - oneWidth, (float) (0 + tempLineHeight / 2.0));
            }
        }

        canvas.drawPath(mPath, mLinePaint);
        canvas.drawCircle(centX, centY, mDotRadius, mDotPaint);
    }
}
